
#include"../../server/Rpc_Server.hpp"


void Add(const Json::Value &req, Json::Value &rsp) {
   
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;

    sleep(1);
    LOG(LogLevel::DEBUG)<<"num1 :" <<num1<<"  num2 : "<<num2<<"  rsp:"<<rsp;
}

int main()
{
    xu::server::RpcRouter::Ptr rr = std::make_shared<xu::server::RpcRouter>();
    xu::server::SDescribeFactory::Ptr sd = std::make_shared<xu::server::SDescribeFactory>();
    sd->SetMethod("Add");
    sd->SetParameter("num1",xu::server::VType::INTEGRAL);
    sd->SetParameter("num2",xu::server::VType::INTEGRAL);
    sd->SetCallback(Add);
    sd->SetRestype(xu::server::VType::INTEGRAL);

    xu::server::RpcServer rs(xu::Address("127.0.0.1",9090),true,xu::Address("127.0.0.1",9999));
    rs.registerMethod(sd->Build());
    rs.Start();
    
    
    return 0;
}



















// auto gdis = xu::DispatcherFactory::create();


// void Rpc (const xu::BaseConnection::Ptr &conn ,const xu::RpcRequest::Ptr &msg)
// {
//     LOG(LogLevel::DEBUG)<<"调用Rpc"<<"method: "<<msg->Method();
// }

// void Topic (const xu::BaseConnection::Ptr &conn ,const xu::TopicRequest::Ptr &msg)
// {
//     LOG(LogLevel::DEBUG)<<"调用Topic\n"<<"key: "<<msg->Key()<<"msg: "<<msg->Msg()<<"mtype:  "<<(int)msg->Mtype();
// }

// void RpcMessageCallback(const xu::BaseConnection::Ptr &bc, xu::BaseMessage::Ptr &msg)
// {
//     std::string body = msg->Serialization();
//     LOG(LogLevel::DEBUG)<<msg->Id() <<" : "<< (int)msg->Mtype()<<" : "<<body;

//     gdis->onMessage(bc,msg);

//     auto res = xu::MessageFactory::create<xu::RpcResponse>();
//     res->SetId(msg->Id());
//     res->SetMType(xu::MType::RSP_RPC);
//     res->SetRcode(xu::RCode::RCODE_OK);
//     res->SetResult(33);
//     bc->Send(res);

// }

// int main()
// {
//     gdis->registerHandler<xu::RpcRequest>(xu::MType::REQ_RPC,Rpc);
//     gdis->registerHandler<xu::TopicRequest>(xu::MType::REQ_TOPIC,Topic);
//     xu::BaseServer::Ptr bs = xu::ModuoServerFactory::create(9999);
//     bs->SetMessageCallback(RpcMessageCallback);
//     bs->Start();

//     return 0;
// }